package in.co.codedoc.json;


public class JSONLongValue 
	extends JSONValue
{
	public JSONLongValue(long value)
	{
		this.value = value;
	}

	public JSONLongValue(JSONValue jsonValue)
	{
		if(jsonValue == null)
		{
			this.value = 0;
		}
		else if(jsonValue instanceof JSONLongValue)
		{
			this.value = ((JSONLongValue)jsonValue).GetValue();
		}
		else if(jsonValue instanceof JSONDoubleValue)
		{
			this.value = (long)(((JSONDoubleValue)jsonValue).GetValue());
		}
		else if(jsonValue instanceof JSONStringValue)
		{
			this.value = Long.parseLong(((JSONStringValue)jsonValue).GetValue());
		}
		else
		{
			throw new RuntimeException("Dont know how to build a JSONLongValue from " + jsonValue.getClass());
		}		
	}
	
	public long GetValue()
	{
		return value;
	}

	@Override
	public StringBuffer Encode(StringBuffer buffer)
	{
		buffer.append(value);
		return buffer;
	}
	
	@Override
	public boolean equals(Object t)
	{
		if((t == null) || !(t instanceof JSONLongValue))
		{
			return false;
		}

		JSONLongValue that = (JSONLongValue)t;
		
		return that.value == value;
	}

	@Override
	public int hashCode()
	{
		return (int)value;
	}
	
	private long value;
}
